
package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.interfaces.OnCheckAnswerListener;
import com.kugou.game.sdk.interfaces.OnRequestListener;
import com.kugou.game.sdk.ui.dialog.BaseConfirmDialog;
import com.kugou.game.sdk.ui.widget.LoadingView;
import com.kugou.game.sdk.utils.Rx;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 描述:通过问题取回密码界面
 *
 * @author wcj
 * @since 2015年8月17日 上午10:18:23
 */
public class GetBackPasswdByProblemFragment extends BaseSDKWorkerFragment {

    private static final int MSG_REFRESH_VIEW = 10000;

    private static final int MSG_COMMIT_ANSWER = 10001;

    private static final int MSG_COMMIT_ANSWER_SUCCESS = 10002;

    private static final int MSG_COMMIT_ANSWER_FAILED = 10003;

    private static final int MSG_INIT_VIEW = 10004;

    private static final int MSG_COMMIT_PASSWD = 10005;

    private static final int MSG_COMMIT_PASSWD_SUCCESS = 10006;

    private static final int MSG_COMMIT_PASSWD_FAILED = 10007;

    private static final int MSG_SHOW_LOADING_VIEW = 10008;

    private static final int MSG_HIDE_LOADING_VIEW = 10009;

    private ScrollView mScrollView;

    private LoadingView mLoadingView;

    private LinearLayout mAnswerLayout, mPasswdLayout;

    private EditText mAnswerEditText, mPasswd1EditText;

    private TextView mUserNameTV, mProblemTV, mPasswdTips;

    private String mKugouUserName;

    private int mProblemId;

    private String mToken, mUnixtime;

    private static String[] sProblems = {
            "你在哪所中学读高中？", "你的宠物叫什么名字？", "你最喜欢的音乐是？", "你最喜欢的球队是？", "你最喜欢的书是？", "你最好的朋友是？",
            "你的出生年月是？", "你爱人的名字是？", "你父亲的名字是？", "你母亲的名字是？"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int viewId = Rx.layout.kg_fragment_get_back_password_by_problem;
        if (SDKControler.getActivityOrientation() == ActivityOrientation.LANDSCAPE) {
            viewId = Rx.layout.kg_fragment_get_back_password_by_problem_land;
        }
        View view = inflater.inflate(viewId, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mScrollView = (ScrollView) view.findViewById(Rx.id.kg_layout_scrollview);

        mLoadingView = (LoadingView) view.findViewById(Rx.id.kg_loading);

        mAnswerLayout = (LinearLayout) view.findViewById(Rx.id.kg_ll_input_answer);

        mPasswdLayout = (LinearLayout) view.findViewById(Rx.id.kg_ll_input_passwd);

        mAnswerEditText = (EditText) view.findViewById(Rx.id.kg_et_input_answer);

        mPasswd1EditText = (EditText) view.findViewById(Rx.id.kg_et_input_passwd1);
        mPasswd1EditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (mPasswdTips.getVisibility() == View.VISIBLE) {
                    mPasswdTips.setVisibility(View.GONE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        mUserNameTV = (TextView) view.findViewById(Rx.id.kg_tv_account);

        mProblemTV = (TextView) view.findViewById(Rx.id.kg_tv_problem);

        mPasswdTips = (TextView) view.findViewById(Rx.id.kg_tv_passwd_tips);

        Button nextStepBtn = (Button) view.findViewById(Rx.id.kg_btn_next_step);
        nextStepBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hideSoftInput(getActivity());
                if (TextUtils.isEmpty(mAnswerEditText.getText().toString())) {
                    showToast("请填写密保问题答案");
                    return;
                }
                sendEmptyBackgroundMessage(MSG_COMMIT_ANSWER);
            }
        });

        Button commitBtn = (Button) view.findViewById(Rx.id.kg_btn_commit_passwd);
        commitBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hideSoftInput(getActivity());
                if (TextUtils.isEmpty(mPasswd1EditText.getText().toString())) {
                    mPasswdTips.setText("请输入密码");
                    mPasswdTips.setVisibility(View.VISIBLE);
                    showToast("请输入密码");
                    return;
                }

                if (mPasswd1EditText.getText().toString().length() < 6
                        || mPasswd1EditText.getText().toString().length() > 16) {
                    mPasswdTips.setText("请输入6-16位字母/数字密码");
                    mPasswdTips.setVisibility(View.VISIBLE);
                    showToast("请输入6-16位字母/数字密码");
                    return;
                }

                sendEmptyBackgroundMessage(MSG_COMMIT_PASSWD);
            }
        });

    }

    public void setData(String userName, int problem) {
        mKugouUserName = userName;
        mProblemId = problem;

        sendEmptyUiMessage(MSG_REFRESH_VIEW);
    }

    public void init() {
        sendEmptyUiMessage(MSG_INIT_VIEW);
    }

    private void showConfimDialog() {
        final BaseConfirmDialog dialog = new BaseConfirmDialog(getActivity());
        dialog.setMessage("密码修改成功");
        dialog.setSuccessLogoVisibility(View.VISIBLE);
        dialog.setTextGravity(Gravity.CENTER);
        dialog.setCancelBtnVisibility(false);
        dialog.setOKBtnText("知道了");
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                getActivity().finish();
            }
        });
        dialog.show();
    }

    @Override
    public void handleUiMessage(Message msg) {
        super.handleUiMessage(msg);

        switch (msg.what) {
            case MSG_REFRESH_VIEW:
                mUserNameTV.setText("酷狗账号：" + mKugouUserName);
                if (mProblemId < sProblems.length) {
                    mProblemTV.setText(Html.fromHtml("<font color=#ff333333>问题：</font>"
                            + "<font color=#249EF4>" + sProblems[mProblemId] + "</font>"));
                } else {
                    mProblemTV.setText("密保问题有误，请联系客服");
                }

                break;
            case MSG_SHOW_LOADING_VIEW:
                hideSoftInput(getActivity());
                mScrollView.setVisibility(View.GONE);
                mLoadingView.setVisibility(View.VISIBLE);
                break;
            case MSG_HIDE_LOADING_VIEW:
                mScrollView.setVisibility(View.VISIBLE);
                mLoadingView.setVisibility(View.GONE);
                break;

            case MSG_COMMIT_ANSWER_FAILED:
                sendEmptyUiMessage(MSG_HIDE_LOADING_VIEW);
                showToast((String) msg.obj);
                break;
            case MSG_COMMIT_ANSWER_SUCCESS:
                sendEmptyUiMessage(MSG_HIDE_LOADING_VIEW);
                mAnswerLayout.setVisibility(View.GONE);
                mPasswdLayout.setVisibility(View.VISIBLE);
                break;
            case MSG_COMMIT_PASSWD_FAILED:
                sendEmptyUiMessage(MSG_HIDE_LOADING_VIEW);
                showToast((String) msg.obj);
                break;
            case MSG_COMMIT_PASSWD_SUCCESS:

                Intent intent = new Intent(SDKActions.ACTION_GET_NEW_PASSWORD);
                intent.putExtra(BaseCommonTitleFragmentActivity.USER_NICKNAME_KEY, mKugouUserName);
                intent.putExtra(BaseCommonTitleFragmentActivity.USER_PASSWORD_KEY, mPasswd1EditText
                        .getText().toString().trim());
                getActivity().sendBroadcast(intent);

                sendEmptyUiMessage(MSG_HIDE_LOADING_VIEW);
                showConfimDialog();
                break;

            case MSG_INIT_VIEW:
                mAnswerLayout.setVisibility(View.VISIBLE);
                mPasswdLayout.setVisibility(View.GONE);
                break;

            default:
                break;
        }
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        super.handleBackgroundMessage(msg);

        switch (msg.what) {
            case MSG_COMMIT_ANSWER:
                sendEmptyUiMessage(MSG_SHOW_LOADING_VIEW);

                UserManager.getInstance().checkAnswer(mKugouUserName,
                        mAnswerEditText.getText().toString(), mProblemId,
                        new OnCheckAnswerListener() {

                            @Override
                            public void onSuccess(String token, String unixtime) {
                                mToken = token;
                                mUnixtime = unixtime;
                                sendEmptyUiMessage(MSG_COMMIT_ANSWER_SUCCESS);
                            }

                            @Override
                            public void onFailed(String errorMsg) {
                                Message msg = Message.obtain();
                                msg.what = MSG_COMMIT_ANSWER_FAILED;
                                msg.obj = errorMsg;
                                sendUiMessage(msg);
                            }
                        });

                break;

            case MSG_COMMIT_PASSWD:
                sendEmptyUiMessage(MSG_SHOW_LOADING_VIEW);

                UserManager.getInstance().getPasswdByProblem(mKugouUserName, mToken, mUnixtime,
                        mPasswd1EditText.getText().toString(), new OnRequestListener() {

                            @Override
                            public void onSuccess() {
                                sendEmptyUiMessage(MSG_COMMIT_PASSWD_SUCCESS);
                            }

                            @Override
                            public void onFailed(String errorMsg) {
                                Message msg = Message.obtain();
                                msg.obj = errorMsg;
                                msg.what = MSG_COMMIT_PASSWD_FAILED;
                                sendUiMessage(msg);
                            }
                        });

            default:
                break;
        }
    }

}
